<?php
session_start();


include_once "fieldspecs_spec.php";
include_once "fieldspecs_mdl.php";
include_once "fieldspecs_view.php";

$maintable = 'fieldspecs';
// get mode
$mode = $_REQUEST['mode'];

if($mode == 'update'){

	$kv = $_REQUEST['kv'];
	$field = $_REQUEST['field'];
	$value = $_REQUEST['value'];
	
	// security checks here...
		
	$keyvalues[0]['fs_id'] = $kv;	
	$newvalues[0][$field] = $value;

	$result = do_update($keyvalues, $newvalues, $maintable);
	//print_r($result);
	print json_encode($result);

}else if($mode == 'add'){
	$fs = new CF_FieldSpec($fieldspec);
	$ar_add_columns = $fs->GetColumnsArray('add');
	$newvalues = CF_ParamReciever::GetReqParamsByList($ar_add_columns,'',false);
	$result = CF_CrudLibPdo::CrudAdd($keyvalues, $newvalues, $maintable);
	print_r($result);

header('Location: fieldspecs_ctl.php?mode=view');	

}else{

	$fs = new CF_FieldSpec($fieldspec);

//	$crudset_id = $_GET['crudset_id'];


	$filters =  CF_FilterReciever::get_filters($fieldspec);
	//$params = CF_FilterQueryPdo::filter_parameters($filters);
	
	$set =  CF_FilterReciever::filters_to_set($filters);
	
	//echo 'LOOKHEER<pre>';
	//print_r($filters);
	//print_r($set);
	//echo '</pre>END';
	$records = get_records($filters);
	
	$ar_columns = $fs->GetColumnsArray('listquery');
	//print 'ar_columns';
	//print_r($ar_columns);

	$ar_lookuplists = get_lookuplists($fs,$ar_columns,$set);
	//echo "<pre>";
	//print_r($ar_lookuplists);
	//echo "</pre>";
	show_listview($records,$fieldspec,$ar_lookuplists,$filters);
	
}


?>